package org.convallaria.infrastruct.ce.properties;

import lombok.Data;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.NestedConfigurationProperty;

import java.time.Duration;
import java.util.HashMap;
import java.util.Map;

/**
 * 缓存配置属性
 * 支持多级缓存配置和细粒度控制
 * 
 * @author gjh
 * @since 2024-09-24
 */
@Data
@ConfigurationProperties(prefix = "convallaria.cache")
public class CacheProperties {

    /**
     * 是否启用缓存
     */
    private boolean enabled = true;

    /**
     * 缓存类型：redis, caffeine, hybrid
     */
    private String type = "hybrid";

    /**
     * 默认缓存配置
     */
    @NestedConfigurationProperty
    private DefaultCacheConfig defaultConfig = new DefaultCacheConfig();

    /**
     * 特定缓存配置
     */
    private Map<String, CacheConfig> caches = new HashMap<>();

    /**
     * Redis配置
     */
    @NestedConfigurationProperty
    private RedisCacheConfig redis = new RedisCacheConfig();

    /**
     * Caffeine配置
     */
    @NestedConfigurationProperty
    private CaffeineCacheConfig caffeine = new CaffeineCacheConfig();

    /**
     * 监控配置
     */
    @NestedConfigurationProperty
    private MonitoringConfig monitoring = new MonitoringConfig();

    @Data
    public static class DefaultCacheConfig {
        /**
         * 默认过期时间
         */
        private Duration ttl = Duration.ofHours(1);

        /**
         * 是否允许空值
         */
        private boolean allowNullValues = true;

        /**
         * 是否启用统计
         */
        private boolean enableStatistics = true;
    }

    @Data
    public static class CacheConfig {
        /**
         * 过期时间
         */
        private Duration ttl;

        /**
         * 最大大小
         */
        private Long maximumSize;

        /**
         * 是否允许空值
         */
        private Boolean allowNullValues;

        /**
         * 是否启用统计
         */
        private Boolean enableStatistics;

        /**
         * 缓存类型：redis, caffeine, hybrid
         */
        private String type;
    }

    @Data
    public static class RedisCacheConfig {
        /**
         * 键前缀
         */
        private String keyPrefix = "convallaria:cache:";

        /**
         * 是否使用键前缀
         */
        private boolean useKeyPrefix = true;

        /**
         * 默认过期时间
         */
        private Duration defaultTtl = Duration.ofHours(1);

        /**
         * 是否启用事务支持
         */
        private boolean enableTransactionSupport = false;

        /**
         * 序列化类型
         */
        private String serializationType = "json";
    }

    @Data
    public static class CaffeineCacheConfig {
        /**
         * 默认最大大小
         */
        private long defaultMaximumSize = 1000;

        /**
         * 默认过期时间
         */
        private Duration defaultTtl = Duration.ofMinutes(30);

        /**
         * 刷新后写入过期时间
         */
        private Duration refreshAfterWrite = Duration.ofMinutes(10);

        /**
         * 是否启用统计
         */
        private boolean enableStatistics = true;
    }

    @Data
    public static class MonitoringConfig {
        /**
         * 是否启用监控
         */
        private boolean enabled = true;

        /**
         * 监控端点路径
         */
        private String endpoint = "/actuator/cache";

        /**
         * 是否启用指标收集
         */
        private boolean enableMetrics = true;

        /**
         * 指标收集间隔
         */
        private Duration metricsInterval = Duration.ofMinutes(1);
    }
}
